package cn.codeforfun.vote;

import cn.codeforfun.follower.Follower;
import cn.codeforfun.follower.FollowerOption;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 投票选项
 */
@Entity
@Table(name = "t_option")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Option {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    private String isInput = "not";
    @Transient
    private String description;
    @Transient
    private Integer amount = 0;
    @Transient
    private Boolean select = false;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "question_id")
    @JsonBackReference
    private Question question;
    @OneToMany(mappedBy = "option")
    public Set<FollowerOption> followerOptions = new LinkedHashSet<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getAmount() {
        Set<Follower> followers = new HashSet<>();
        for (FollowerOption followerOption : getFollowerOptions()) {
            followers.add(followerOption.getFollower());
        }
        return followers.size();
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Set<FollowerOption> getFollowerOptions() {
        return followerOptions;
    }

    public void setFollowerOptions(Set<FollowerOption> followerOptions) {
        this.followerOptions = followerOptions;
    }

    public Boolean getSelect() {
        return select;
    }

    public void setSelect(Boolean select) {
        this.select = select;
    }

    public String getIsInput() {
        return isInput;
    }

    public void setIsInput(String isInput) {
        this.isInput = isInput;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
